package com.sinosoft.jwjw.base;

import com.sinosoft.jwjw.utils.ConvertUtil;
import com.sinosoft.jwjw.utils.ResultDetail;
import com.sinosoft.jwjw.utils.ResultPage;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Date;
import java.util.Map;


/**
 *
 * @param <T>
 * @param <PK>
 */
public abstract class BaseCrudController<T,PK extends Serializable> extends BaseController {
    public abstract BaseCrudService<T,PK > getBaseService();

    /**
     * 单表对象插入
     * 前置条件：每个类都要有id，无id不可用
     */
    @RequestMapping("/insert")
    @ResponseBody
    public String insert(){

        RequestParamUtils();
        ResultDetail result = new ResultDetail();

        String id = ConvertUtil.toString(params.get("id"));
        if("".equals(id)){
            id = ConvertUtil.getUUID20();
            params.put("id",id);
        }
        T t = toObject(params.toString());
        int rows = getBaseService().insert(t);
        if(rows > 0){
            result.setCode(0);
            result.setMsg("保存成功");
            result.getResult().put("id",id);
        }
        else{
            result.setCode(500);
            result.setMsg("保存失败");
        }

        return  result.toString();
    }

    /**
     * 单表对象所有字段更新
     * 前置条件：每个类都要有id，无id不可用
     */
    @RequestMapping("/update")
    @ResponseBody
    public String update(){

        RequestParamUtils();
        ResultDetail result = new ResultDetail();

        String id = ConvertUtil.toString(params.get("id"));
        if("".equals(id)){
            result.setCode(501);
            result.setMsg("id缺失，无法更新。");
            return  result.toString();
        }
        T t = toObject(params.toString());
        int rows = getBaseService().updateByPrimaryKey(t);
        if(rows > 0){
            result.setCode(0);
            result.setMsg("保存成功");
            result.getResult().put("id",id);
        }
        else{
            result.setCode(500);
            result.setMsg("保存失败");
        }
        return  result.toString();
    }


    /**
     * 根据id更新表字段
     * @return
     */
    @RequestMapping("/updateById")
    @ResponseBody
    public String updateById(){

        RequestParamUtils();
        ResultDetail result = new ResultDetail();

        String id = ConvertUtil.toString(params.get("id"));

        params.put("modifyDate",new Date());
        params.put("modifyName","操作人姓名");
        params.put("modifyId","操作人id");
        if("".equals(id)){
            result.setCode(500);
            result.setMsg("缺少id，无法更新");
            return  result.toString();
        }
        result = getBaseService().updateById(params);
        return  result.toString();
    }
    /**
     * 单个对象查询的方法
     * @return
     */
    @RequestMapping("/detail")
    @ResponseBody
    public String detail(){

        //获取参数
        RequestParamUtils();
        ResultDetail result = new ResultDetail();

        //解析处理参数
        String id = ConvertUtil.toString(params.get("id"));//

        T t = getBaseService().selectByPrimaryKey(id);

        if (t == null) {
            result.setCode(500);
            result.setMsg("对象不存在，请确认");
        } else {
            result.setCode(0);
            result.setMsg("成功");
            result.setResult(ConvertUtil.toJSONObject(t));
        }
        return result.toString();
    }

    /**
     * 物理删除
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(){

        RequestParamUtils();
        ResultDetail result = new ResultDetail();

        //解析处理参数
        String id = ConvertUtil.toString(params.get("id"));//

        int rows = getBaseService().deleteByPrimaryKey(id);
        if(rows > 0){
            result.setCode(0);
            result.setMsg("保存成功");
            result.getResult().put("id",id);
        }
        else{
            result.setCode(500);
            result.setMsg("保存失败");
        }

        return  result.toString();
    }

    /**
     * 逻辑删除
     * @return
     */
    @RequestMapping("/deleteById")
    @ResponseBody
    public String deleteById(){

        RequestParamUtils();
        ResultDetail result = new ResultDetail();

        //解析处理参数
        String id = ConvertUtil.toString(params.get("id"));//

        result = getBaseService().deleteById(id);
        return  result.toString();
    }

    /**
     * 逻辑删除(批量)
     * params = {
     *  ids = ["id1","id2","id3"]
     * }
     * @return
     */
    @RequestMapping("/deleteByIds")
    @ResponseBody
    public String deleteByIds(){

        RequestParamUtils();
        ResultDetail result = new ResultDetail();

        //解析处理参数
        JSONArray ids = (JSONArray)params.get("ids");

        result = getBaseService().deleteByIds(ids);
        return  result.toString();
    }


    /**
     * 常规sql列表分页查询
     * params  必传参数：
     *      * @pageindex int 默认1
     *      * @pagesize int 默认10
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/querylist" , method = RequestMethod.POST)
    public String querylist() {

        //获取参数
        RequestParamUtils();
        ResultPage resultPage = new ResultPage();

        // 解析处理参数
        // 参数认为前端传入无问题
        // 注意sql注入问题
        // String visitStartTime = ConvertUtil.toString(params.get("visitStartTime"));//yyyy-MM-dd HH:mm:ss
        // String visitEndTime = ConvertUtil.toString(params.get("visitEndTime"));//yyyy-MM-dd HH:mm:ss
        // String testText = ConvertUtil.toString(params.get("testText"));

        resultPage = getBaseService().getListPage(params);

        return resultPage.toString();
    }
}
